package me.jessyan.armscomponent.app.mvp.ui.activity.learn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.paginate.Paginate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerAddPlanComponent;
import me.jessyan.armscomponent.app.mvp.contract.learn.AddPlanContract;
import me.jessyan.armscomponent.app.mvp.model.entity.PlanColumn;
import me.jessyan.armscomponent.app.mvp.presenter.learn.AddPlanPresenter;
import me.jessyan.armscomponent.app.mvp.ui.widget.MyRadioGroup;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description: 我的学习计划-建议学习 115
 * <p>
 * Created by MVPArmsTemplate on 08/27/2021 22:27
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@Route(path = RouterHub.LEARNING_ADDPLANACTIVITY)
public class AddPlanActivity extends BaseActivity<AddPlanPresenter> implements AddPlanContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.courseDetailLy)
    LinearLayout courseDetailLy;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    @Inject
    RecyclerView.Adapter mAdapter;

    private Paginate mPaginate;
    private boolean isLoadingMore;

    Map <String, Map> amountMap = new HashMap<>(); //栏目选择集合
    List<PlanColumn>column1 = new ArrayList<>();
    List<PlanColumn>column2 = new ArrayList<>();
    List<PlanColumn>column3 = new ArrayList<>();
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAddPlanComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.learning_activity_add_plan; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbarTitle.setText("增加计划");
        initRecyclerView();
        mRecyclerView.setAdapter(mAdapter);
        initPaginate();

        PlanColumn planColumn1 = new PlanColumn();
        planColumn1.setId("1");
        planColumn1.setName("综合排序");
        column1.add(planColumn1);

        PlanColumn planColumn2 = new PlanColumn();
        planColumn2.setId("2");
        planColumn2.setName("播放最多");
        column1.add(planColumn2);

        PlanColumn planColumn3 = new PlanColumn();
        planColumn3.setId("3");
        planColumn3.setName("评分最高");
        column1.add(planColumn3);

        PlanColumn planColumn4 = new PlanColumn();
        planColumn4.setId("4");
        planColumn4.setName("基础学院");
        column2.add(planColumn4);

        PlanColumn planColumn5 = new PlanColumn();
        planColumn5.setId("5");
        planColumn5.setName("法学院");
        column2.add(planColumn5);

        PlanColumn planColumn6 = new PlanColumn();
        planColumn6.setId("6");
        planColumn6.setName("宣教学院");
        column2.add(planColumn6);

        PlanColumn planColumn7 = new PlanColumn();
        planColumn7.setId("7");
        planColumn7.setName("伊斯兰研究与阿拉伯语学院");
        column2.add(planColumn7);

        PlanColumn planColumn8 = new PlanColumn();
        planColumn8.setId("8");
        planColumn8.setName("《古兰经》与古兰学");
        column3.add(planColumn8);

        PlanColumn planColumn9 = new PlanColumn();
        planColumn9.setId("9");
        planColumn9.setName("圣训与圣训学");
        column3.add(planColumn9);

        PlanColumn planColumn10 = new PlanColumn();
        planColumn10.setId("10");
        planColumn10.setName("教义学");
        column3.add(planColumn10);

        PlanColumn planColumn11 = new PlanColumn();
        planColumn11.setId("11");
        planColumn11.setName("伊斯兰哲学");
        column3.add(planColumn11);

        PlanColumn planColumn12 = new PlanColumn();
        planColumn12.setId("12");
        planColumn12.setName("逻辑学");
        column3.add(planColumn12);

        PlanColumn planColumn13 = new PlanColumn();
        planColumn13.setId("13");
        planColumn13.setName("道德功修");
        column3.add(planColumn13);

        PlanColumn planColumn14 = new PlanColumn();
        planColumn14.setId("14");
        planColumn14.setName("西方语言学");
        column3.add(planColumn14);

        filledLayout();

    }

    @Override
    public void showLoading() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    private void filledLayout(){
        amountMap.clear();//清除支付列表

        courseDetailLy.removeAllViews();
        LinearLayout consumeDinnerDetailLy = (LinearLayout) getLayoutInflater().inflate(R.layout.learning_add_plan_column_radio, null);
        MyRadioGroup radioGroup_column1 = consumeDinnerDetailLy.findViewById(R.id.radioGroup_column1);
        MyRadioGroup radioGroup_column2 = consumeDinnerDetailLy.findViewById(R.id.radioGroup_column2);
        MyRadioGroup radioGroup_column3 = consumeDinnerDetailLy.findViewById(R.id.radioGroup_column3);

        for (PlanColumn column : column1){
            RadioButton radioButton = (RadioButton) getLayoutInflater().inflate(R.layout.learning_add_planl_radio_button, null);
            radioButton.setId(Integer.parseInt(column.getId()));
            radioButton.setText(column.getName());
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        //String id = (String) buttonView.getTag(R.id.radio_id);
                        //Map<String , String> valueMap = new HashMap<>();
                        //valueMap.put("id", id);
                        //valueMap.put("amount", amount);
                        //amountMap.put(modeType, valueMap);
                        //calculateTotal();
                    }
                }
            });
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5,10,25,10);
            radioGroup_column1.addView(radioButton, params);
        }
        for (PlanColumn column : column2){
            RadioButton radioButton = (RadioButton) getLayoutInflater().inflate(R.layout.learning_add_planl_radio_button, null);
            radioButton.setId(Integer.parseInt(column.getId()));
            radioButton.setText(column.getName());
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        //String id = (String) buttonView.getTag(R.id.radio_id);
                        //Map<String , String> valueMap = new HashMap<>();
                        //valueMap.put("id", id);
                        //valueMap.put("amount", amount);
                        //amountMap.put(modeType, valueMap);
                        //calculateTotal();
                    }
                }
            });
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5,10,25,10);
            radioGroup_column2.addView(radioButton, params);
        }

        for (PlanColumn column : column3){
            RadioButton radioButton = (RadioButton) getLayoutInflater().inflate(R.layout.learning_add_planl_radio_button, null);
            radioButton.setId(Integer.parseInt(column.getId()));
            radioButton.setText(column.getName());
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        //String id = (String) buttonView.getTag(R.id.radio_id);
                        //Map<String , String> valueMap = new HashMap<>();
                        //valueMap.put("id", id);
                        //valueMap.put("amount", amount);
                        //amountMap.put(modeType, valueMap);
                        //calculateTotal();
                    }
                }
            });
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5,10,25,10);
            radioGroup_column3.addView(radioButton, params);
        }

        radioGroup_column1.check(Integer.parseInt(column1.get(0).getId()));
        radioGroup_column2.check(Integer.parseInt(column2.get(0).getId()));
        radioGroup_column3.check(Integer.parseInt(column3.get(0).getId()));


        courseDetailLy.addView(consumeDinnerDetailLy);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onRefresh() {
        mPresenter.requestDailyList(true);
    }

    /**
     * 开始加载更多
     */
    @Override
    public void startLoadMore() {
        isLoadingMore = true;
    }

    /**
     * 结束加载更多
     */
    @Override
    public void endLoadMore() {
        isLoadingMore = false;
    }

    @Override
    public Activity getActivity() {
        return this;
    }
    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        ArmsUtils.configRecyclerView(mRecyclerView, mLayoutManager);
    }

    @Override
    protected void onDestroy() {
        DefaultAdapter.releaseAllHolder(mRecyclerView);//super.onDestroy()之后会unbind,所有view被置为null,所以必须在之前调用
        super.onDestroy();
        this.mPaginate = null;
    }

    /**
     * 初始化Paginate,用于加载更多
     */
    private void initPaginate() {
        if (mPaginate == null) {
            Paginate.Callbacks callbacks = new Paginate.Callbacks() {
                @Override
                public void onLoadMore() {
                    mPresenter.requestDailyList(false);
                }

                @Override
                public boolean isLoading() {
                    return isLoadingMore;
                }

                @Override
                public boolean hasLoadedAllItems() {
                    return false;
                }
            };

            mPaginate = Paginate.with(mRecyclerView, callbacks)
                    .setLoadingTriggerThreshold(0)
                    .build();
            mPaginate.setHasMoreDataToLoad(false);
        }
    }
}
